﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Golos.Helper;
using Golos.Http;
using Golos.Users.UserCache;

namespace Golos.BackEnd.Client
{
    public class UsersClient : HttpClientBase
    {
        public UsersClient(Uri serverUri) : base(serverUri)
        {
        }

        public UsersClient() : base()
        {
            serverUri = new Uri(string.Format("http://{0}/", Topology.GetEndPoint(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"settings\topology\users"))));
        }

        public UserInfo GetUserInfoByUserId(Guid userId)
        {
            var response = TrySendRequest(string.Format("?operation=userinfo&userid={0}", userId), "GET");
            if (response.ResponseStream == null || response.StatusCode != HttpStatusCode.OK)
                return null;
            return UserInfo.Deserialize(response.ResponseStream);
        }

        public Guid RegisterUser(string name, string email, UserIdType userIdType, string identificationId)
        {
            var response = TrySendRequest(string.Format("?operation=register&idtype={0}&indid={1}&name={2}&email={3}", (int)userIdType, identificationId, Convert.ToBase64String(Encoding.UTF8.GetBytes(name)), Convert.ToBase64String(Encoding.UTF8.GetBytes(email))), "POST");
            if (response.ResponseStream == null || response.StatusCode != HttpStatusCode.OK)
                throw new Exception("Can't register user");
            byte[] data = new byte[response.ContentLength];
            response.ResponseStream.Read(data, 0, data.Length);
            return new Guid(data);
        }

        public Guid RegisterUser(string name, UserIdType userIdType, string identificationId)
        {
            var response = TrySendRequest(string.Format("?operation=register&idtype={0}&indid={1}&name={2}", (int)userIdType, identificationId, Convert.ToBase64String(Encoding.UTF8.GetBytes(name))), "POST");
            if (response.ResponseStream == null || response.StatusCode != HttpStatusCode.OK)
                throw new Exception("Can't register user");
            byte[] data = new byte[response.ContentLength];
            response.ResponseStream.Read(data, 0, data.Length);
            return new Guid(data);
        }

        public Guid RegisterUser(UserIdType userIdType, string identificationId)
        {
            var response = TrySendRequest(string.Format("?operation=register&idtype={0}&indid={1}", (int)userIdType, identificationId), "POST");
            if (response.ResponseStream == null || response.StatusCode != HttpStatusCode.OK)
                throw new Exception("Can't register user");
            byte[] data = new byte[response.ContentLength];
            response.ResponseStream.Read(data, 0, data.Length);
            return new Guid(data);
        }

        public void AttachUser(Guid userId, UserIdType userIdType, string identificationId, bool saveActivityToSocialWeb)
        {
            var response = TrySendRequest(string.Format("?operation=attach&yserid={0}&idtype={1}&indid={2}&saveactivity={3}", userId, (int)userIdType, identificationId, saveActivityToSocialWeb), "POST");
            if (response.ResponseStream == null || response.StatusCode != HttpStatusCode.OK)
                throw new Exception("Can't attach user");
        }

        public bool UpdateProfile(UserInfo userInfo)
        {
            try
            {
                var response = TrySendRequest("?operation=updateprofile", "POST", userInfo.Serialize());
                return response != null && response.StatusCode == HttpStatusCode.OK;
            }
            catch (Exception exception)
            {
                LogClass.WriteErrorEntry("Can't create poll {0}", exception.ToString());
                return false;
            }
        }
    }
}
